package com.vhall.component.controller.account.api;


import com.alibaba.fastjson.JSONObject;
import com.vhall.component.controller.account.facade.api.ApiAuthFacade;
import com.vhall.component.entity.account.dto.ExchangeSessionIdDTO;
import com.vhall.component.entity.account.vo.*;
import com.vhall.component.framework.common.annotation.VhallComponent;
import com.vhall.component.plugin.common.annotation.ApiSignatureAuth;
import com.vhall.component.plugin.common.annotation.TokenAuth;
import com.vhall.component.plugin.common.support.Token;
import com.vhall.component.plugin.common.support.TokenThreadLocal;
import com.vhall.component.service.account.AccountService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * api用户 - 登录、登出、获取登录用户详情
 *
 * @author dalong
 * @date 2021-04-25 16:46
 */

@VhallComponent
public class ApiAuthController implements ApiAuthFacade {

    @Autowired
    private AccountService accountService;


    @ApiSignatureAuth
    @Override
    public AuthLoginRspVO login(@Validated AuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.authLogin(authLoginVO, request, response, true);
    }


    @Override
    public void logout(@Validated AuthLogoutVO authLogoutVO) {
        accountService.authLogout(authLogoutVO);
    }


    @Override
    public AuthLoginRspVO loginWatch(@Validated AuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.authLogin(authLoginVO, request, response, true);
    }

    @Override
    public AuthLoginRspVO loginAssistant(AssistantLoginVO assistantLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        AuthLoginVO authLoginVO = new AuthLoginVO();
        BeanUtils.copyProperties(assistantLoginVO, authLoginVO);
        //这里是嘉宾 助理登录，  因为 嘉宾助理 登录 属于新的单独的接口   account表没有拆
        //对于 account表来说  type 只有  1   2   3
        //因为 后台  account表里面的 account_type  只有  1  2  3
        //  其中 1代表主持人  2代表 观众   3代表游客
        authLoginVO.setAssistant("as");
        authLoginVO.setType(2);
        return accountService.authLogin(authLoginVO, request, response, false);
    }

    @Override
    public AuthLoginRspVO loginAnchor(AnchorLoginVO vo) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.loginAnchor(vo, request, response);
    }

    @Override
    public AuthLoginRspVO loginWatchSso(@Validated WatchLoginSsoVO vo) {

        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.loginWatchSso(vo, request, response);
    }


    @Override
    public ThirdAuthLoginRspVO thirdLogin(@Validated ThirdAuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.thirdAuthLogin(authLoginVO, request, response);
    }


    @Override
    public VisitorAuthLoginRspVO visitor(@Validated VisitorAuthLoginVO authLoginVO) {
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        Assert.notNull(servletRequestAttributes, "参数异常");
        HttpServletRequest request = servletRequestAttributes.getRequest();
        HttpServletResponse response = servletRequestAttributes.getResponse();
        return accountService.visitor(authLoginVO, request, response);
    }

    @Override
    public List<NicknamesVO> getRandomNicknames() {
        return accountService.getRandomNicknames();
    }

    /**
     * 根据token获取用户信息
     *
     * @return token緩存的信息
     */
    @Override
    @TokenAuth
    public Token getInfoBytoken() {
        return TokenThreadLocal.getToken();
    }

    @Override
    public JSONObject exchangeSessionId(ExchangeSessionIdDTO ticket) {
        return accountService.exchangeSessionId(ticket);
    }

}
